
#include <iostream>
#include <boost/thread.hpp>
#include <boost/process.hpp>
#include <boost/version.hpp>
#include <boost/config.hpp>
using namespace std;
// boost库提供了boost::process::child类来创建新进程
namespace bp = boost::process;

void wait1(int seconds)
{
    boost::this_thread::sleep(boost::posix_time::seconds(seconds));
}

void thread1()
{
    try
    {
        for (int i = 0; i < 5; ++i)
        {
            wait1(1);
            std::cout << i << std::endl;
        }
    }
    catch (boost::thread_interrupted &)
    {
    }
}
int main1()
{
    cout << BOOST_VERSION << endl;
    cout << BOOST_LIB_VERSION << endl;
    cout << BOOST_PLATFORM << endl;
    cout << BOOST_COMPILER << endl;
    cout << BOOST_STDLIB << endl;
    boost::thread t(thread1);
    wait1(3);
    t.interrupt();
    t.join();
}


void subProcess() {
    cout << "sub process" << endl;
}
    
int main2() {
    vector<string> args; // 子进程命令行参数
    args.push_back("myprogram");
    bp::child c(bp::exe = "./myprogram", bp::args = args); // 创建子进程
    c.wait(); // 等待子进程结束
    cout << "parent  process---------------" << endl;
    subProcess(); // 在主进程中继续执行一些操作
    return 0;
}

using namespace std;
class MyClass {
public:
    void threadFunc() {
        cout << "Hello, world!" << endl;
    }

    void startThread() {
        boost::thread t(&MyClass::threadFunc, this); // 创建线程，并绑定对象及成员函数
        t.join();
    }
};
    
int main3() {
    MyClass obj;
    obj.startThread();
    return 0;
}


class MyClass1 {
public:
    void threadFunc() {
        cout << "Hello, world!" << endl;
    }
};
    
class Wrapper {
public:
   Wrapper(MyClass1* obj) : m_obj(obj) {}
   void startThread() {
       boost::thread t(boost::bind(&MyClass1::threadFunc, m_obj)); // 使用boost::bind来绑定对象及成员函数，创建线程
       t.join();
   }
private:
   MyClass1* m_obj;
};
    
int main() {
    MyClass1 obj;
    Wrapper wrap(&obj);
    wrap.startThread();
    return 0;
}